package com.nulldev.util.net.sdtp;

import java.io.File;

import com.nulldev.util.io.FileUtil;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.net.IPUtil;
import com.nulldev.util.net.packets.NetInfoAdapter;
import com.nulldev.util.net.packets.NetPacket;
import com.nulldev.util.net.packets.PacketManager;
import com.nulldev.util.net.tcp.TCPCore;
import com.nulldev.util.net.tcp.server.TCPServer;

@Deprecated
/**
 * Replaced by project ANN
 * @author null
 *
 */
public class SDTPServer extends TCPServer{

	private boolean allowSDTP = true;
	public SDTPType[] SUPPORTED_TYPES = {SDTPType.HTML_PROXY};
	public String HTML_SERVER_DATA = "<html><head><p style='font-family:sans-serif;'>openSDTP v2</p><h3 style='font-family:sans-serif;'>Default Place holder.</h3></head></html>";
	public File FTP_FOLDER = FileUtil.safeCreate(getLocation(""));
	public SDTPServer() {
		super("sdtp.server", "SDTPServer", IPUtil.nextFreePort());
	}
	
	public SDTPServer(final boolean open) {
		super("sdtp.server", "SDTPServer", IPUtil.nextFreePort(), !open);
	}
	
	public SDTPServer(final int port, final boolean open) {
		super("sdtp.server", "SDTPServer", MathUtil.clamp(10, 65530, port), !open);
	}

	@Override
	public void receive(TCPCore connection, String event, String payload, byte[] bytes, long length, final long readTime) {
		final NetPacket packet = PacketManager.parsePacket(this, connection, event);
		//System.out.println("Recieved Packet '" + packet.getPacketName() + "'.");
		if (!packet.isGeneric()) {
			//System.out.println("Calling event...");
			packet.server(packet.getData(), event, NetInfoAdapter.create(connection));
//			if (packet.getPacketName().equals("Ping Packet")) {
//				final NetPacket resposne = PacketManager.byID("gen.pong");
//				resposne.server(packet.getData(), event, connection);
//				System.out.println("Responding....");
//			}else {
//				packet.server(packet.getData(), event, connection);
//			}			
		}
	}
	
	public String getLocation(final String location) {
		if (location.equals("")) {
			return System.getProperty("user.home") + "/FTP/";
		}else {
			return location;
		}
	}
	
//	public boolean isSupported(final SDTPType type) {
//		return ArrayUtils.contains(this.SUPPORTED_TYPES, type);
//	}
//
//	public boolean isSDTPenabled() {
//		return this.allowSDTP;
//	}
//	
//	public void setSDTPenabled(final boolean enabled) {
//		this.allowSDTP = enabled;
//	}
}
